package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Client;
import cn.wolfcode.crm.domain.StudentTracking;
import cn.wolfcode.crm.query.StudentTrackingQueryObject;
import cn.wolfcode.crm.service.IClientService;
import cn.wolfcode.crm.service.ISchoolService;
import cn.wolfcode.crm.service.IStudentTrackingService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("studentTracking")
public class StudentTrackingController {

    @Autowired
    private IStudentTrackingService studentTrackingService;
    @Autowired
    private IClientService clientService;
    @Autowired
    private ISchoolService schoolService;

    @RequestMapping("view")
    @RequiresPermissions(value = {"studentTracking:view", "学员跟踪列表"}, logical = Logical.OR)
    public String view() {
        return "studentTracking/studentTracking";
    }

    @ResponseBody
    @RequestMapping("selectAll")
    public List<StudentTracking> selectAll() {
        List<StudentTracking> trackings = studentTrackingService.selectAll();
        return  trackings;
    }

    @ResponseBody
    @RequestMapping("query")
    public PageResult query(StudentTrackingQueryObject qo) {
        return studentTrackingService.query(qo);
    }

    @ResponseBody
    @RequestMapping("saveOrUpdate")
    public JSONResult saveOrUpdate(StudentTracking studentTracking) {
        try {
            if (studentTracking.getId() != null) {
                studentTrackingService.updateByPrimaryKey(studentTracking);
                clientService.updateByStudentTracking(studentTracking.getClient());

            } else {
                studentTrackingService.insert(studentTracking);
            }
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("操作失败");
        }
    }

    @RequestMapping("changeState")
    @ResponseBody
    @RequiresPermissions(value = {"studentTracking:changeState", "学员跟踪审核"}, logical = Logical.OR)
    public JSONResult changeState(Long id) {
        try {
            studentTrackingService.changState(id);
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("操作失败");
        }
    }

    @ResponseBody
    @RequestMapping("trackingSave")
    public JSONResult trackingSave(Client client) {
        try {
            //保存到表中
            studentTrackingService.trackingSave(client,new Date());
            //改变跟踪状态,添加跟踪次数
            clientService.updateByTracking(client.getId());
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("操作失败");
        }
    }
}
